#ifndef EXAMPLE_JOBS_H
#define EXAMPLE_JOBS_H

#include "JobInterface.h"
#include "Logger.h"
#include <string>
#include <chrono>
#include <thread>
#include <random>
#include <sstream>
#include <atomic>
#include <map>
#include <mutex>

// 声明线程局部存储的线程名称
extern thread_local std::string currentThreadName;

// 任务执行计数器
namespace {
    std::map<std::string, std::shared_ptr<std::atomic<int>>> jobCounters;
    std::mutex countersMutex;
    
    int getAndIncrementCounter(const std::string& jobId) {
        std::lock_guard<std::mutex> lock(countersMutex);
        auto it = jobCounters.find(jobId);
        if (it == jobCounters.end()) {
            jobCounters[jobId] = std::make_shared<std::atomic<int>>(0);
            it = jobCounters.find(jobId);
        }
        return it->second->fetch_add(1) + 1;
    }
}

/**
 * @brief 示例任务1：简单任务
 */
class SimpleJob : public JobInterface {
public:
    SimpleJob(const std::string& id, std::chrono::milliseconds period, 
               std::chrono::milliseconds maxExecutionTime) 
        : id_(id), period_(period), maxExecutionTime_(maxExecutionTime) {}
    
    void execute() override {
        // 增加任务执行计数
        int count = getAndIncrementCounter(id_);
        
        // 记录任务开始执行日志
        std::ostringstream startLog;
        startLog << "任务 " << id_ << " (SimpleJob) [" << count << "] 在线程 " << currentThreadName << " 开始执行";
        Logger::getInstance().info(startLog.str());
        
        // 模拟任务执行
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        
        // 记录任务结束执行日志
        std::ostringstream endLog;
        endLog << "任务 " << id_ << " (SimpleJob) [" << count << "] 在线程 " << currentThreadName << " 执行结束";
        Logger::getInstance().info(endLog.str());
    }
    
    std::chrono::milliseconds getPeriod() const override {
        return period_;
    }
    
    std::chrono::milliseconds getMaxExecutionTime() const override {
        return maxExecutionTime_;
    }
    
    std::string getId() const override {
        return id_;
    }

private:
    std::string id_;
    std::chrono::milliseconds period_;
    std::chrono::milliseconds maxExecutionTime_;
};

/**
 * @brief 示例任务2：耗时任务
 */
class HeavyJob : public JobInterface {
public:
    HeavyJob(const std::string& id, std::chrono::milliseconds period, 
             std::chrono::milliseconds maxExecutionTime) 
        : id_(id), period_(period), maxExecutionTime_(maxExecutionTime) {}
    
    void execute() override {
        // 增加任务执行计数
        int count = getAndIncrementCounter(id_);
        
        // 记录任务开始执行日志
        std::ostringstream startLog;
        startLog << "任务 " << id_ << " (HeavyJob) [" << count << "] 在线程 " << currentThreadName << " 开始执行";
        Logger::getInstance().info(startLog.str());
        
        // 模拟耗时任务执行
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        
        // 记录任务结束执行日志
        std::ostringstream endLog;
        endLog << "任务 " << id_ << " (HeavyJob) [" << count << "] 在线程 " << currentThreadName << " 执行结束";
        Logger::getInstance().info(endLog.str());
    }
    
    std::chrono::milliseconds getPeriod() const override {
        return period_;
    }
    
    std::chrono::milliseconds getMaxExecutionTime() const override {
        return maxExecutionTime_;
    }
    
    std::string getId() const override {
        return id_;
    }

private:
    std::string id_;
    std::chrono::milliseconds period_;
    std::chrono::milliseconds maxExecutionTime_;
};

/**
 * @brief 示例任务3：随机执行时间任务
 */
class RandomJob : public JobInterface {
public:
    RandomJob(const std::string& id, std::chrono::milliseconds period, 
               std::chrono::milliseconds maxExecutionTime) 
        : id_(id), period_(period), maxExecutionTime_(maxExecutionTime) {
        // 初始化随机数生成器
        randomEngine_.seed(std::chrono::system_clock::now().time_since_epoch().count());
        distribution_ = std::uniform_int_distribution<int>(10, 300);
    }
    
    void execute() override {
        // 随机睡眠时间
        int sleepTime = distribution_(randomEngine_);
        
        // 增加任务执行计数
        int count = getAndIncrementCounter(id_);
        
        // 记录任务开始执行日志
        std::ostringstream startLog;
        startLog << "任务 " << id_ << " (RandomJob) [" << count << "] 在线程 " << currentThreadName << " 开始执行，预计执行时间: " << sleepTime << "ms";
        Logger::getInstance().info(startLog.str());
        
        std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
        
        // 记录任务结束执行日志
        std::ostringstream endLog;
        endLog << "任务 " << id_ << " (RandomJob) [" << count << "] 在线程 " << currentThreadName << " 执行结束";
        Logger::getInstance().info(endLog.str());
    }
    
    std::chrono::milliseconds getPeriod() const override {
        return period_;
    }
    
    std::chrono::milliseconds getMaxExecutionTime() const override {
        return maxExecutionTime_;
    }
    
    std::string getId() const override {
        return id_;
    }

private:
    std::string id_;
    std::chrono::milliseconds period_;
    std::chrono::milliseconds maxExecutionTime_;
    std::default_random_engine randomEngine_;
    std::uniform_int_distribution<int> distribution_;
};

/**
 * @brief 示例任务4：可能抛出异常的任务
 */
class ExceptionJob : public JobInterface {
public:
    ExceptionJob(const std::string& id, std::chrono::milliseconds period, 
                 std::chrono::milliseconds maxExecutionTime) 
        : id_(id), period_(period), maxExecutionTime_(maxExecutionTime) {}
    
    void execute() override {
        // 增加任务执行计数
        int count = getAndIncrementCounter(id_);
        
        // 记录任务开始执行日志
        std::ostringstream startLog;
        startLog << "任务 " << id_ << " (ExceptionJob) [" << count << "] 在线程 " << currentThreadName << " 开始执行";
        Logger::getInstance().info(startLog.str());
        
        if (shouldThrow_) {
            // 记录异常日志
            std::ostringstream errorLog;
            errorLog << "任务 " << id_ << " (ExceptionJob) [" << count << "] 在线程 " << currentThreadName << " 抛出异常";
            Logger::getInstance().error(errorLog.str());
            throw std::runtime_error("任务执行异常");
        }
        // 正常执行
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        
        // 记录任务结束执行日志
        std::ostringstream endLog;
        endLog << "任务 " << id_ << " (ExceptionJob) [" << count << "] 在线程 " << currentThreadName << " 执行结束";
        Logger::getInstance().info(endLog.str());
    }
    
    std::chrono::milliseconds getPeriod() const override {
        return period_;
    }
    
    std::chrono::milliseconds getMaxExecutionTime() const override {
        return maxExecutionTime_;
    }
    
    std::string getId() const override {
        return id_;
    }

private:
    std::string id_;
    std::chrono::milliseconds period_;
    std::chrono::milliseconds maxExecutionTime_;
    bool shouldThrow_;
};

#endif // EXAMPLE_JOBS_H